package text;

import model.FAT;
import model.File;
import model.Folder;
import model.OpenFiles;
import service.FATService;
import util.FileSystemUtil;

import java.util.List;
import java.util.Scanner;

public class text01 {
    public static String path = "D:";
    public static FATService fatService = null;
    public static FAT[] myFAT = null;
    public static OpenFiles openFiles=null;
    public static void main(String[] args) {
        fatService = new FATService();
        fatService.initFAT();
        myFAT=FATService.getMyFAT();
        openFiles=FATService.getOpenFiles();
//        提示当前路径
//        showPath();
//        展示当前文件夹下的内容
        showContent();
//        提示界面
        tishi();
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        while (true) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    showPath();
                    break;
                case 1:
                    showContent();
                    break;
                case 2:
                    cd();
                    break;
                case 3:
                    last();
                    break;
                case 4:
                   create();
                    break;
                case 5:
                    rename();
                    break;
                case 6:
                   delete();
                    break;
                case 7:
                    openFile();
                    break;
                case 8:
                    removeFile();
                    break;
                case 9:
                    showOpenFile();
                    break;
                case 10:
                    showFATS();
                    break;
                case 11:
                    System.exit(1);
                    break;
            }
//        提示界面
            tishi();
        }
    }

//    展示已经打开的文件
    private static void showOpenFile() {
        System.out.println("已经打开的文件：");
        for (int i = 0; i < openFiles.getFiles().size(); i++) {
            File file = openFiles.getFiles().get(i).getFile();
            System.out.println(file.getFileName()+"  "+ file.getLocation());
        }
    }

//    关闭文件
    private static void removeFile() {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入当前目录要关闭的文件名称：");
        String name = scanner.next();
        FAT fat =fatService.getFileFAT(path,name);
        fatService.removeOpenFile(fat);
        System.out.println("文件关闭成功！");
        showPath();
    }

//    打开文件
    private static void openFile() {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入当前目录要打开的文件名称：");
        String name = scanner.next();
        FAT fat =fatService.getFolderFAT(path,name);
        if(fat.getType()==FileSystemUtil.FOLDER){
            System.out.println("当前只能打开文件！");
            return;
        }
        fatService.addOpenFile(fat,0);
        System.out.println("文件打开成功！");
        showPath();
    }

    //    删除操作
    private static void delete() {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入当前目录要删除的文件名称：");
        String name = scanner.next();
        FAT fat =fatService.getFileFAT(path,name);
        fatService.delete(fat);
        showPath();
    }

    //        展示文件分配表
    private static void showFATS() {
        for (int i = 3; i < myFAT.length; i++) {
            Object obj =myFAT[i].getObject();
            if(myFAT[i].getType()==FileSystemUtil.FOLDER){
                Folder folder =(Folder)obj;
                System.out.println("文件夹："+folder.getFolderName()+"  "+folder.getLocation());
            }else if(myFAT[i].getType()==FileSystemUtil.FILE){
                File file =(File)obj;
                System.out.println("文件："+file.getFileName()+"  "+file.getLocation());
            }
        }
    }

    //    重命名
    private static void rename() {
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入当前目录要修改的文件名称：");
        String oldName = scanner.next();
        System.out.println("请输入新的文件名称：");
        String newName = scanner.next();
        FAT fat =fatService.getFolderFAT(path,oldName);
        if(fat.getType()==FileSystemUtil.FILE){
            File file=(File)fat.getObject();
            file.setFileName(newName);
        }else if(fat.getType()==FileSystemUtil.FOLDER){
            Folder folder =(Folder)fat.getObject();
            folder.setFolderName(newName);
        }
        fatService.modifyLocation(path+"\\"+oldName,path+"\\"+newName);

        showPath();
    }

    //    新建文件 文件夹
    private static void create() {
        System.out.println("请输入要创建的文件类型 1文件 2文件夹");
        Scanner scanner =new Scanner(System.in);
        int index = scanner.nextInt();
        int i;
        if(index==1){
           i= fatService.creatFile(path);
            System.out.println("新建文件成功！ 文件名："+((File)myFAT[i].getObject()).getFileName());
        }else if(index==2){
            i=fatService.creatFolder(path);
            System.out.println("新建文件夹成功！ 文件夹名："+((Folder)myFAT[i].getObject()).getFolderName());
        }else {
            System.out.println("输入有误！");
        }
    }

    //    返回上一级目录并展示内容
    private static void last() {
        if(!path.equals("D:")) {
            String[] pathArray = path.split("\\\\");
            path = "";
            for (int i = 0; i < pathArray.length - 1; i++) {
                if(i==pathArray.length-2){
                    path = path + pathArray[i];
                }else {
                    path = path + pathArray[i]+"\\";
                }
            }
        }
        showPath();
        List<FAT> faTs = fatService.getFATs(path);
        if (faTs.size() == 0)
            System.out.println("该文件夹下没有内容！");
        else {
            System.out.println("该文件夹下内容：");
        }
        for (int i = 0; i < faTs.size(); i++) {
            if (faTs.get(i).getType() == FileSystemUtil.FILE) {
                System.out.println(((File) faTs.get(i).getObject()).getFileName());
            } else if (faTs.get(i).getType() == FileSystemUtil.FOLDER) {
                System.out.println(((Folder) faTs.get(i).getObject()).getFolderName());
            }
        }
    }

    //    展示当前文件夹下的内容
    private static void showContent() {
        showPath();
        List<FAT> faTs = fatService.getFATs(path);
        if (faTs.size() == 0)
            System.out.println("该文件夹下没有内容！");
        else  System.out.println("该文件夹下内容：");
        for (int i = 0; i < faTs.size(); i++) {
            if (faTs.get(i).getType() == FileSystemUtil.FILE) {
                System.out.println(((File) faTs.get(i).getObject()).getFileName());
            } else if (faTs.get(i).getType() == FileSystemUtil.FOLDER) {
                System.out.println(((Folder) faTs.get(i).getObject()).getFolderName());
            }
        }
    }

    //    展示路径
    private static void showPath() {
        System.out.println(path);
    }

    //    打开指定的文件夹并展示内容
    private static void cd() {
        System.out.println("请输入要打开的文件夹名：");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();
        path = path + "\\" + input;
        showPath();
        List<FAT> faTs = fatService.getFATs(path);
        if (faTs.size() == 0)
            System.out.println("该文件夹下没有内容！");
        else  System.out.println("该文件夹下内容：");
        for (int i = 0; i < faTs.size(); i++) {
            if (faTs.get(i).getType() == FileSystemUtil.FILE) {
                System.out.println(((File) faTs.get(i).getObject()).getFileName());
            } else if (faTs.get(i).getType() == FileSystemUtil.FOLDER) {
                System.out.println(((Folder) faTs.get(i).getObject()).getFolderName());
            }
        }
    }


    //    提示操作
    private static void tishi() {
        System.out.println("---------------------------------------------------");
        System.out.println("0.展示当前路径");
        System.out.println("1.显示当前目录下的内容");
        System.out.println("2.打开文件夹");
        System.out.println("3.回退上一级目录");
        System.out.println("4.新建文件 文件夹/文件类型 ");
        System.out.println("5.文件重命名");
        System.out.println("6.删除");
        System.out.println("7.打开文件");
        System.out.println("8.关闭文件");
        System.out.println("9.展示已打开的文件");
        System.out.println("10.展示文件分配表");
        System.out.println("11.退出程序");
        System.out.println("---------------------------------------------------");
    }
}
